// bird类
const birdDom = document.querySelector('.bird')
const birdStyles = getComputedStyle(birdDom)
const birdWidth = parseFloat(birdStyles.width)
const birdHeight = parseFloat(birdStyles.height)

class Bird extends RectTangle {
    constructor() {
        // 继承父类的属性和方法
        super(birdWidth, birdHeight, 300, 250, 0, 0, birdDom)
        this.g = 12000 // 加速度
        this.swingStatus = 1 // 翅膀的状态
        this.timer = null // 翅膀的计时器
        this.birdMaxTop = skyHeight - landHeight - birdHeight
    }

    // 重写move方法
    move(duration) {
        super.move(duration)
        this.ySpeed = this.g * duration
    }

    // bird特有的跳跃方法
    jump() {
        this.ySpeed = -3000
    }

    // 扇动翅膀方法
    startWave() {
        if (this.timer) {
            return
        }
        this.timer = setInterval(() => {
            this.swingStatus = this.swingStatus + 1
            if (this.swingStatus > 3) {
                this.swingStatus = 1
            }
            this.render()
        }, 200);
    }

    // 停止翅膀方法
    stopWave() {
        clearInterval(this.timer)
        this.timer = null
    }

    // 重写render方法
    render() {
        // 重用父类render逻辑代码
        super.render()
        this.dom.className = `bird swing${this.swingStatus}`
    }

    onMove() {
        if (this.top >= (skyHeight - landHeight - birdHeight)) {
            this.top = skyHeight - landHeight - birdHeight
        } else if (this.top <= 0) {
            this.top = 0
        }
    }
}

// var bird = new Bird

// setInterval(() => {
//     bird.move(16 / 1000)
// }, 16);

// window.addEventListener("keydown", function (e) {
//     if (e.key === ' ') {
//         // console.log('按下了空格键');
//         bird.jump()
//         bird.startWave()
//     }
// })